package com.widevision.ListActivity;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import net.widevision.android.R;
import net.widevision.android.TwitterApp;
import twitter4j.ResponseList;
import twitter4j.User;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.widevision.adapter.FollowerListAdapter;
import com.widevision.adapter.FollowerListBean;

public class FollowerName extends Activity{
	
	private static final String twitter_consumer_key = "JXmXcE5yFPLIRCCtQ79eg";
	private static final String twitter_secret_key = "dnVojAwaewvEt8E2QwLYtavwm8cdUHwph7lZCaQPMY";
	
	//***********************************//
	private AQuery aq;
	//***********************************//
	
    FollowerListAdapter frndListAdapter;
	
    FollowerListBean followerListBean;
    
    ArrayList<Drawable> followerimg;
    
    ArrayList<Object> itemfollowerlist;
    
    ResponseList<twitter4j.User> followerlisttext;
    
    User user,user1;
    
    TwitterApp mTwitter;
    
    InputStream is;
    
    Drawable followersimg;
    
    ListView followerlistview;
    
    TextView Scroll;
    
    String followername,screeName;
    
    int Userid,Id,id,count;
    
    String userId,userid;
    
    	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		//***********************************//
		aq = new AQuery(this);
		//***********************************//
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.followerlist);
		
		followerlistview    =(ListView) findViewById(R.id.frndidlist);
		Scroll              =(TextView) findViewById(R.id.scroller1);
       
		Bundle bundle=getIntent().getExtras();
			
		Userid=bundle.getInt("USERID");

		prepareArralist();
		
		frndListAdapter  = new FollowerListAdapter(FollowerName.this, itemfollowerlist,aq);
		
		followerlistview.setAdapter(frndListAdapter);
		
		Scroll.setSelected(true);
		
		Scroll.setText("@"+screeName+"'s Followers"+"                                                      ");
		
		followerlistview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
				
				TextView txt = (TextView) view.findViewById(R.id.userId);
				userid=txt.getText().toString();
			    Intent intent=new Intent(FollowerName.this, UserProfile.class);
			    id=Integer.parseInt(userid);
			    intent.putExtra("USERID", id);
			    startActivity(intent);
			}
		});
	}

	private void prepareArralist() {
		
		    itemfollowerlist=new ArrayList<Object>();
		    
		    mTwitter 	= new TwitterApp(this, twitter_consumer_key,twitter_secret_key);
		    
			if (mTwitter.hasAccessToken()){
				
			user=mTwitter.showUser(Userid);
			
			screeName=user.getScreenName();
			
			count=user.getFollowersCount();
			
			followerlisttext=mTwitter.Followerids(Userid);
			
			try {
				for(int i=0;i<count;i++){
					
				user1=followerlisttext.get(i);
				
				Id=user1.getId();
				
				userId=String.valueOf(Id);
				
				followername=user1.getScreenName();
			
				URL u=user1.getProfileImageURL();
				String followersimg=String.valueOf(u);
				Log.d("image string value>>>>>>>>>>>>", followersimg);
//				try {
//						is = u.openStream();
//					} catch (IOException e) {
//						e.printStackTrace();
//					}
//					
//				followersimg = Drawable.createFromStream(is, "src name");

				AddObjectToList(followername,followersimg,userId);
				
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void AddObjectToList(String followername,String followersimg,String userId) {
		followerListBean=new FollowerListBean();
		followerListBean.setName(followername);
		followerListBean.setImage(followersimg);
		followerListBean.setUserId(userId);
		itemfollowerlist.add(followerListBean);
		
	}
  }
